package com.sqx.modules.goods.controller;

import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.goods.entity.Goods;
import com.sqx.modules.goods.entity.GoodsAttr;
import com.sqx.modules.goods.entity.GoodsClassify;
import com.sqx.modules.goods.entity.GoodsShop;
import com.sqx.modules.goods.service.GoodsClassifyService;
import com.sqx.modules.goods.service.GoodsService;
import com.sqx.modules.goods.service.GoodsShopService;
import com.sqx.modules.sys.controller.AbstractController;
import com.sqx.modules.sys.entity.SysUserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags={"商户端-商品"})
@RestController
@RequestMapping("/admin/goods")
public class GoodsController extends AbstractController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsShopService goodsShopService;
    @Autowired
    private GoodsClassifyService goodsClassifyService;

    @ApiOperation(value = "添加商品")
    @PostMapping(value = "/insertGoods")
    public Result insertGoods(@RequestBody Goods goods){

        return goodsService.insertGoods(goods);
    }

    @ApiOperation(value = "修改商品")
    @PostMapping("/update")
    public Result update(@RequestBody Goods goods){
        return goodsService.updateGoods(goods);
    }

    @ApiOperation("查看所有商品")
    @GetMapping(value = "/selectGoodsList")
    public Result selectGoodsList(Integer page, Integer limit, Integer classifyId, String goodsName, Integer status, Long shopId, String shopName){

        return goodsService.selectAdminGoodsList(page, limit, classifyId, goodsName, status, shopId, shopName);
    }

    @ApiOperation("删除商品")
    @PostMapping(value = "deleteGoodsById")
    public Result deleteGoodsById(Long goodsId){

        return goodsService.deleteGoodsById(goodsId);
    }

    @ApiOperation("给商铺批量添加商品")
    @PostMapping(value = "shopAddGoods")
    public Result shopAddGoods(Long shopId, List goodsIdList){

        return goodsService.shopAddGoods(shopId, goodsIdList);
    }

    @ApiOperation(value = "查看商品详情")
    @GetMapping("/selectGoodsById")
    public Result selectGoodsById(Long goodsId) {

        return goodsService.selectGoodsById(goodsId);
    }

    @ApiOperation(value = "单规格生成sku")
    @GetMapping(value = "/onlyFormatAttr")
    public Result onlyFormatSku(@ApiParam("商品图片")@RequestParam(required = false) String coverImg,
                                @ApiParam("原价")@RequestParam(required = false) String originalPrice,
                                @ApiParam("售价")@RequestParam(required = false) String price){
        return goodsService.onlyFormatAttr(coverImg, originalPrice, price);
    }

    @ApiOperation(value = "多规格生成sku")
    @PostMapping(value = "/isFormatAttr")
    public Result isFormatSku(@RequestBody GoodsAttr attr,
                              @ApiParam("商品图片")@RequestParam(required = false) String coverImg,
                              @ApiParam("原价")@RequestParam(required = false) String originalPrice,
                              @ApiParam("售价")@RequestParam(required = false) String price){
        return goodsService.isFormatAttr(attr, coverImg, originalPrice, price);
    }

    @ApiOperation(value = "回显属性")
    @GetMapping(value = "/formatAttr")
    public Result formatAttr(Long goodsId){
        return goodsService.formatAttr(goodsId);
    }

    @ApiOperation(value = "回显规格")
    @GetMapping(value = "/findAttrValue")
    public Result findAttrValue(Long goodsId){
        return goodsService.findAttrValue(goodsId);
    }

    @ApiOperation("查看所有商铺")
    @GetMapping(value = "selectAllShop")
    public Result selectAllShop(Integer page, Integer limit, String shopName){

        return goodsService.selectAllShop(page, limit, shopName);
    }

    @ApiOperation("查看所有商铺(不分页)")
    @GetMapping(value = "selectAllShopList")
    public Result selectAllShopList(){
        /*SysUserEntity user = getUser();
        Long userId = user.getUserId();*/
        return goodsService.selectAllShopList(null);
    }

    @ApiOperation("添加商铺")
    @PostMapping(value = "insertGoodsShop")
    public Result insertGoodsShop(@RequestBody GoodsShop goodsShop){

        return goodsShopService.insertGoodsShop(goodsShop);
    }

    @ApiOperation("修改商铺信息")
    @PostMapping(value = "updateGoodsShop")
    public Result updateGoodsShop(@RequestBody GoodsShop goodsShop){

        return goodsShopService.updateGoodsShop(goodsShop);
    }

    @ApiOperation("删除商铺")
    @GetMapping(value = "deleteGoodsShop")
    public Result deleteGoodsShop(Long id){

        return goodsShopService.deleteGoodsShop(id);
    }

    @ApiOperation("获取所有商品类型")
    @GetMapping(value = "selectAllClassify")
    public Result selectAllClassify(Integer page,Integer limit,String shopName, Long shopId){
        return goodsService.selectAllClassify(page,limit,shopName, shopId);
    }

    @ApiOperation("添加商品类型")
    @PostMapping(value = "insertClassify")
    public Result insertClassify(@RequestBody GoodsClassify goodsClassify){

        return goodsClassifyService.insertClassify(goodsClassify);
    }

    @ApiOperation("修改商品类型")
    @PostMapping(value = "updateClassify")
    public Result updateClassify(@RequestBody GoodsClassify goodsClassify){

        return goodsClassifyService.updateClassify(goodsClassify);
    }

    @ApiOperation("删除商品类型")
    @GetMapping(value = "deleteClassify")
    public Result deleteClassify(Long classifyId){

        return goodsClassifyService.deleteClassify(classifyId);
    }


}
